<template>
	<view :class="['page', { iphoneX: isIphoneX }]">
		<template v-if="token && isLogin">
			<template v-if="loadingState.default">
				<loading :loadingState="loadingState"></loading>
			</template>
			<template v-else>
				<view class="body">
					<view class="change-btn">
						<view class="select-data">设备名称：{{ dev_name }}</view>
					</view>

					<view class="form-wrap m-t2">
						<view class="form-item">
							<view class="label">{{params.work_mode?'手动开关':'自动开关'}}</view>
							<view class="detail-switch">
								<switch color="#3399FF" :checked="params.work_mode" disabled @click="saveSetting('work_mode')"></switch>
								<!-- <view class="state">{{ params.work_mode? '开' : '关' }}</view> -->
							</view>
						</view>
						<view class="form-item">
							<view class="label">出水开关</view>
							<view class="detail-switch">
								<switch color="#3399FF" :checked="params.water_sw" disabled @click="saveSetting('water_sw')"></switch>
								<view class="state">{{ params.water_sw? '开' : '关' }}</view>
							</view>
						</view>
						<view class="form-item">
							<view class="label" style="width: auto;">土湿度预警最大值</view>
							<view class="detail-switch">
								<input v-model.number="params.soil_h_warn_max" @blur="saveSetting('soil_h_warn_max')" class="uni-input" type="number" placeholder="请输入预警值" style="font-size: 30rpx;height: 32px;text-align: end;padding-right: 10px;" />
							</view>
						</view>

						<view class="form-item">
							<view class="label" style="width: auto;">土湿度预警最小值</view>
							<view class="detail-switch">
								<input v-model.number="params.soil_h_warn_min" @blur="saveSetting('soil_h_warn_min')" class="uni-input" type="number" placeholder="请输入预警值" style="font-size: 30rpx;height: 32px;text-align: end;padding-right: 10px;" />
							</view>
						</view>

					</view>
				</view>
			</template>
		</template>
	</view>
</template>

<script>
	import formdemo from '@/components/form.vue';
	import loading from '@/components/loading-more.vue';
	export default {
		name: 'name',
		data() {
			return {
				dev_id: '',
				dev_name: '',
				isLogin: false,
				isIphoneX: getApp().globalData.isIphoneX,
				appid: getApp().globalData.appid,
				token: uni.getStorageSync('token'),
				userInfo: uni.getStorageSync('userInfo'),
				loginStateUpdate: false,
				isStop: true,
				loadingState: {
					default: true
				},
				isSetting: false,
				detail: {
					work_mode: '0'
				},
				params: {
					work_mode: 0,
					water_sw: '',
					soil_h_warn_min: '',
					soil_h_warn_max: '',
					user_id: '',
					device_id: '',
					appid: '',
					token: ''
				},
				work_mode_ops: [{
						value: '0',
						name: '手动开关'
					},
					{
						value: '1',
						name: '自动开关'
					}
				],
				time: [{
					start: '00:00',
					end: '00:00'
				}, {
					start: '00:00',
					end: '00:00'
				}, {
					start: '00:00',
					end: '00:00'
				}, {
					start: '00:00',
					end: '00:00'
				}, {
					start: '00:00',
					end: '00:00'
				}, {
					start: '00:00',
					end: '00:00'
				}, {
					start: '00:00',
					end: '00:00'
				}, {
					start: '00:00',
					end: '00:00'
				}],
				select: -1,
				camera_interval_data: [10, 20, 30, 40, 50, 60]
			};
		},
		async onLoad(params) {
			this.dev_id = params.dev_id;
			this.dev_name = params.dev_name;
			if (this.token) {
				this.isLogin = await this.$loginState('check');
				if (this.isLogin) {
					this.detail = await this.getDeviceSetDetail();
					this.setParams();
				} else {
					this.isLogin = await this.$loginState('reset');
				}
			}
		},
		async onShow() {
			if (this.loginStateUpdate) {
				this.token = uni.getStorageSync('token');
				this.userInfo = uni.getStorageSync('userInfo');
				this.isLogin = true;
				this.detail = await this.getDeviceSetDetail();
				this.setParams();
				this.loginStateUpdate = false;
			}

			if (!this.token && !this.isLogin) {
				this.isLogin = await this.$loginState('reset');
			}
		},
		methods: {
			// 获取设备管理详情
			getDeviceSetDetail: async function() {
				let res = await this.$api.tool_api.getGgDetail({
					appid: this.appid,
					token: this.token,
					device_id: this.dev_id,
					user_id: this.userInfo.uid
				}).finally(() => {
					this.loadingState.default = false;
				});
				if (res.data.ret == 1000) {
					return res.data.data.list;
				}
			},

			// 获取设备管理参数
			setParams: function() {
				Object.assign(this.params, {
					work_mode: this.detail.work_mode == 1,
					water_sw: this.detail.water_sw == 1,
					soil_h_warn_min: this.detail.soil_h_warn_min,
					soil_h_warn_max: this.detail.soil_h_warn_max,
				})
			},

			// 设置开始时间
			changeTime: function(e, type, ix) {
				this.select = ix
				if (type == 1) {
					this.time[ix].start = e.detail.value;
				} else if (type == 2) {
					this.time[ix].end = e.detail.value;
				}
				this.saveSetting(3)
				// this.params[`working_hours${ix}`] = this.time[ix].start + '-' + this.time[ix].end;

			},

			// 打开设置
			showSetting: function() {
				this.isSetting = !this.isSetting;
			},

			// 保存设置
			saveSetting: async function(t, ix) {
				uni.showLoading({
					title: '保存中',
					mask: true
				});

				let params = {
					appid: this.appid,
					token: this.token,
					device_id: this.dev_id,
					user_id: this.userInfo.uid,
				}

				if (t == 'work_mode' || t == 'water_sw') {
					params = Object.assign({}, params, {
						[t]: this.params[t] ? 0 : 1
					})
				} else if (t == 'soil_h_warn_min' || t == 'soil_h_warn_max') {
					params = Object.assign({}, params, {
						[t]: this.params[t]
					})
				}

				let res = await this.$api.tool_api.setGgManage(params).finally(() => {
					uni.hideLoading();
				});
				if (res.data.ret == 1000) {
					this.$showToast({
						icon: 'success',
						title: '设置成功'
					});
					this.detail = await this.getDeviceSetDetail();
					this.setParams();
				} else {
					this.$showToast({
						icon: 'error',
						title: '设置失败'
					});
				}
			}
		},
		components: {
			loading
		},
		computed: {},
		watch: {}
	};
</script>

<style lang="less">
	.submit-btn {
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		background: #0085ff;
		color: #fff;
		font-size: 32rpx;
	}

	.work-time {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 60rpx;
		line-height: 60rpx;
		font-size: 28rpx;

		.start,
		.over {
			flex-grow: 2;
			border: 0.1rpx solid #ddd;
			text-align: center;
		}

		.line {
			margin: 0 10rpx;
		}

		.disabled {
			background: #f5f5f5;
			color: #999;
		}
	}
</style>